/*
 * @Author: qinuoyun
 * @Date:   2019-11-11 14:10:22
 * @Last Modified by:   sean
 * @Last Modified time: 2020-01-02 15:42:40
 */

const pages = require(ROOT_DIR + "/pages/home/goods.json");
const cheerio = require('cheerio')
const shriek = require('./shriek')

function getDOM(tag, content = "") {
    let dom = `<${tag}>${content}</${tag}>`;
    return cheerio.load(dom, {
        withDomLvl1: true,
        normalizeWhitespace: false,
        xmlMode: true,
        decodeEntities: false
    })
}

const $ = getDOM("template")



class demo {
    index() {
        let path = ROOT_DIR + DS + "pages" + DS + pages.config.data.path + DS + pages.config.data.name + ".vue";

        console.log("path", path)

        this.create($("template"), pages.config)
        console.log("tag1", $.html());
        let script = getDOM("script", this.script(pages.main.libraries))

        ///script.append(this.script(pages.main.libraries))
        console.log("tag2", script.html());

        let vue = $.html() + script.html();

        to_mkdir(path, vue, true, true);

        return 1123;
    }

    nihao() {
        return 1234;
    }

    test() {
        this.setAction({
            "title": "CCC",
            "name": "ccc",
            "type": "POST",
            "handle": "insert",
            "model": "asdsad",
            "fields": "aaa",
            "where": "bbb"
        });
        return 111;
    }

    /**
     * [test3 description]
     * @return {[type]} [description]
     * this.$cloud.where('zhenhao')
     */
    test3() {
        let _this = shriek("this", "$cloud");

        let _name = shriek("value", "zhenhao");

        let _where = shriek("chained", "where", _this.ast()).append(_name.ast());

        console.log("this", _where.js());


        return "你好";
    }

    test2() {
        // return this.setData({
        //     ddd: "dasd",
        //     csaasd: 13123,
        //     sadjlk: [{
        //         asdjs: "cccc",
        //     }]
        // })
        // this.setComponents({
        //     "nihao": "ceshi",
        //     "tesr-d": "ceajl"
        // })
        return 1;
    }

    /**
     * 设置函数方法
     * @param {[type]} data [description]
     */
    setMethods(data) {
        console.log("读取信息", data);
        let _data = shriek("method", "methods", false, false);
        for (let i in data) {
            let item = data[i];
            let _function = shriek("function", item.name);
            let _action = this.setAction(item);
            _function.append(_action.ast());
            _data.append(_function.ast());
        }
        let _ast = shriek("export");
        _ast.append(_data.ast());
        console.log("AST", _ast.js());
        return _data;
    }

    /**
     * 设置数据对象
     * @param {[type]} data [description]
     */
    setData(data) {
        data = shriek().jsonToAst(data);
        let _ast = shriek("export");
        let _object = shriek("object");
        let _return = shriek("return");
        let _data = shriek("method", "data", true, false);
        for (let i in data) {
            let item = data[i];
            _object.append(item);
        }
        _return.append(_object.ast());
        _data.append(_return.ast());
        _ast.append(_data.ast());
        console.log("AST", _ast.js());
        return _data;
    }

    /**
     * 设置加载组件
     * @param {[type]} data [description]
     */
    setComponents(data) {
        let _data = shriek("method", "components", false, false);
        for (let i in data) {
            let item = data[i];
            let _name = shriek("value", i);
            let _value = shriek("name", item);
            let _label = shriek("label", _name.ast(), _value.ast());
            _data.append(_label.ast());
        }
        let _ast = shriek("export");
        _ast.append(_data.ast());
        console.log("AST", _ast.js());
        return _data;
    }

    /**
     * JS-SQL语句实现
     * @param {[type]} data [description]
     * [
     * {
     *     "title": "CCC",
     *     "name": "ccc",
     *     "type": "POST",
     *     "handle": "insert",
     *     "fields": "aaa",
     *     "page":"1,20",
     *     "where": "bbb"
     * }]
     * this.$cloud.where('zhenhao').then(response => {})
     * @return {[type]} [description]
     */
    setAction(data) {
        //let _this = shriek("this", "$cloud");
        //函数内容使用_use.append(); 进行添加
        let _use = shriek("use", "response");

        let _this = shriek("this", "$cloud");

        let value = "";
        let name = "";

        //设置模型名称-必填*
        value = shriek("value", data.model);
        name = shriek("chained", "name", _this.ast()).append(value.ast());

        //设置查询条件-非必填
        if (data.handle !== 'insert') {
            value = shriek("this", data.where);
            name = shriek("chained", "where", name.ast()).append(value.ast());
        }

        //设置字段
        if (data.handle == 'first' || data.handle == 'select') {
            value = shriek("this", data.fields);
            name = shriek("chained", "fields", name.ast()).append(value.ast());
        }

        //设置分页
        if (data.handle == 'select') {
            value = shriek("value", data.page);
            name = shriek("chained", "page", name.ast()).append(value.ast());
        }

        //设置最后的事件
        if (data.handle == 'insert' || data.handle == 'update') {
            value = shriek("this", data.fields);
            name = shriek("chained", data.handle, name.ast()).append(value.ast());
        } else {
            name = shriek("chained", data.handle, name.ast());
        }
        //这里是结尾

        let _then = shriek("chained", "then", name.ast()).append(_use.ast());

        console.log("this", _then.js());

        return _then;
    }

    script(libraries) {
        console.log("执行我的吗");
        this.body = {
            type: "Program",
            sourceType: "module",
            body: [{
                type: "ExportDefaultDeclaration",
                declaration: {
                    type: "ObjectExpression",
                    properties: []
                }
            }]
        }
        //console.log("libraries", libraries)
        let models = libraries[0].data;
        let gathers = libraries[1].data;
        let actions = libraries[2].data;
        console.log("gathers", gathers);

        let data = {};
        //获取data对象标准格式数据
        for (let i in gathers) {
            let item = gathers[i];
            console.log("item", item.name)
            data[item.name] = {};
            if (item.fields) {
                for (let _i in item.fields) {
                    let _item = item.fields[_i];
                    data[item.name][_item.field] = _item.default;
                }
            }
        }

        let _ast = shriek("export");

        _ast.append(this.setData(data).ast());
        _ast.append(this.setMethods(actions).ast());
        console.log("AST", _ast.js());
        return _ast.js();
    }


    /**
     * HMLT生成器
     * @return {[type]} [description]
     */
    create(DOM, page) {
        if (page.data.class && !empty(page.data.class)) {
            this.class(DOM, page.data.class);
        }
        if (page.data.bind && !empty(page.data.bind)) {
            this.bind(DOM, page.data.bind);
        }
        if (page.data.directive && !empty(page.data.directive)) {
            this.directive(DOM, page.data.directive);
        }
        if (page.data.attribute && !empty(page.data.attribute)) {
            this.attribute(DOM, page.data.attribute);
        }
        if (page.data.action && !empty(page.data.action)) {
            this.action(DOM, page.data.action);
        }
        if (page.data.style && !empty(page.data.style)) {
            this.style(DOM, page.data.style);
        }
        if (page.data.options && !empty(page.data.options)) {
            this.options(DOM, page.data.options);
        }
        for (let i in page.slot) {
            let item = page.slot[i];
            let SON = getDOM(item.name);
            this.create(SON(item.name), item);
            DOM.append(SON.html());
        }
    }

    /**
     * 样式类处理
     * @return {[type]} [description]
     */
    class(DOM, data) {
        for (let i in data) {
            let item = data[i];
            DOM.addClass(item)
        }
    }

    /**
     * 内联样式处理
     * @return {[type]} [description]
     */
    style(DOM, data) {
        data = JSON.stringify(data);
        data = data.replace(/,/g, ";");
        data = data.replace(/"/g, "");
        data = data.replace(/"/g, "");
        data = ltrim(data, "{")
        data = rtrim(data, "}")
        DOM.attr("style", data)
    }

    /**
     * 数据绑定处理
     * @return {[type]} [description]
     * 
     */
    bind(DOM, data) {
        for (let i in data) {
            let item = data[i];
            if (i == 'value') {
                DOM.attr("v-model", item)
            } else {
                DOM.attr(":" + i, item)
            }
        }
    }

    /**
     * 指令绑定处理
     * @return {[type]} [description]
     */
    directive(DOM, data) {
        for (let i in data) {
            let item = data[i];
            DOM.attr("v-" + item, item)
        }
    }

    /**
     * 属性绑定处理
     * @return {[type]} [description]
     */
    attribute(DOM, data) {
        for (let i in data) {
            let item = data[i];
            DOM.attr(i, item)
        }
    }

    /**
     * 事件绑定处理
     * @return {[type]} [description]
     * {
     *  name: "demo",
     *  type: "click"
     * }
     */
    action(DOM, data) {
        for (let i in data) {
            let item = data[i];
            DOM.attr("@" + item.type, item.name)
        }
    }

    /**
     * 选项添加处理
     * @return {[type]} [description]
     */
    options(DOM, data) {
        if (data.type == "static") {
            let SON = getDOM(data.tag);
            for (var i in data.children) {
                let item = data.children[i];
                this.attribute(SON(data.tag), item);
                DOM.append(SON.html());
            }
        }
        if (data.type == "dynamic") {
            let attribute = {};
            attribute["v-for"] = "`(item,i) in ${data.children.data}`"
            attribute[":key"] = "i";
            attribute[":label"] = "item." + data.children.label;
            attribute[":value"] = "item." + data.children.value;
            this.attribute(SON(data.tag), attribute);
            DOM.append(SON.html());
        }

    }


}


module.exports = demo;